﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.BL;
using System.Data;
using MandalaySoft.Common;

public partial class School_MarkingPeriod : System.Web.UI.Page
{
    MyError _error;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session[Global.Session_SchoolID] != null)
            {
                BindMP(-1, 1000);
            }
            else
            {
                Response.Redirect("~/default.aspx");
            }
        }
    }

    

    private void ClearMPForm()
    {
        hidMPID.Value = "-1";
        hidParentMPID.Value = "-1";
        txtTitle.Text = string.Empty;
        txtShortName.Text = string.Empty;
        txtStartDate.Text = string.Empty;
        txtEndDate.Text = string.Empty;
        txtGPStartDate.Text = string.Empty;
        txtGPEndDate.Text = string.Empty;
        chkGraded.Checked = false;
        chkExam.Checked = false;
        chkComment.Checked = false;
    }

    private void ShowMPForm(Int16 Type)
    {
        tbcMP.Visible = true;
        if (Type == 500)
        {
            divSemesterList.Visible = true;
            gvSemester.SelectedIndex = -1;
            divQuarterList.Visible = false;
            gvQuarter.SelectedIndex = -1;
            divProgressList.Visible = false;
        }
        else if (Type == 250)
        {
            divQuarterList.Visible = true;
            gvQuarter.SelectedIndex = -1;
            divProgressList.Visible = false;
            gvProgress.SelectedIndex = -1;
        }
        else if (Type == 125)
        {
            divProgressList.Visible = true;
            gvProgress.SelectedIndex = -1;
        }
    }

    protected void gvYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearMPForm();
        int _MPID = Convert.ToInt32(gvYear.SelectedRow.Cells[0].Text);
        ShowMPForm(500);
        ShowMP(_MPID);
        BindMP(_MPID, 500);
    }

    private void ShowMP(int MPID)
    {
        MarkingPeriod _item = new MarkingPeriod();
        MarkingPeriodDB _db = new MarkingPeriodDB();

        _item = _db.GetItem(MPID);

        if (_item.MPID > 0)
        {
            hidMPID.Value = _item.MPID.ToString();
            hidParentMPID.Value = _item.ParentMPID.ToString();
            hidMPType.Value = _item.Type.ToString();

            txtTitle.Text = _item.Title;
            txtShortName.Text = _item.ShortName;
            txtStartDate.Text = _item.StartDate.ToString("dd-MMM-yyyy");
            txtEndDate.Text = _item.EndDate.ToString("dd-MMM-yyyy");
            txtGPStartDate.Text = _item.GradePostStartDate.ToString("dd-MMM-yyyy");
            txtGPEndDate.Text = _item.GradePostEndDate.ToString("dd-MMM-yyyy");
            chkGraded.Checked = _item.IsGraded;
            chkExam.Checked = _item.IsExam;
            chkComment.Checked = _item.IsComment;
        }

        lblMessage.Text = _item.Error.ErrorMessage;
    }

    private void BindMP(int ParentMPID, Int16 Type)
    {
        try
        {
            MarkingPeriodDB _db = new MarkingPeriodDB();
            DataTable _dt = new DataTable("tblMPList");
            _error = new MyError();
            _dt = _db.GetMarkingPeriodList(ParentMPID, Convert.ToInt32(Session[Global.Session_SchoolID].ToString()), Type, out _error);

            if (Type == 1000)
            {
                gvYear.DataSource = _dt;
                gvYear.DataBind();
            }
            else if (Type == 500)
            {
                gvSemester.DataSource = _dt;
                gvSemester.DataBind();
            }
            else if (Type == 250)
            {
                gvQuarter.DataSource = _dt;
                gvQuarter.DataBind();
            }
            else if (Type == 125)
            {
                gvProgress.DataSource = _dt;
                gvProgress.DataBind();
            }

            if (_error.ErrorNo != 0)
            {
                lblMessage.Text = _error.ErrorMessage;
            }

        }
        catch (Exception Ex)
        {
            lblMessage.Text = "Error at BindSubject(): " + Ex.Message;
        }
    }

    protected void gvSemester_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearMPForm();
        int _MPID = Convert.ToInt32(gvSemester.SelectedRow.Cells[0].Text);
        ShowMPForm(250);
        ShowMP(_MPID);
        BindMP(_MPID, 250);
    }

    protected void gvQuarter_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearMPForm();
        int _MPID = Convert.ToInt32(gvQuarter.SelectedRow.Cells[0].Text);
        ShowMPForm(125);
        ShowMP(_MPID);
        BindMP(_MPID, 125);
    }

    protected void gvProgress_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearMPForm();
        int _MPID = Convert.ToInt32(gvProgress.SelectedRow.Cells[0].Text);
        ShowMPForm(0);
        ShowMP(_MPID);
    }

    protected void btnNewProgress_Click(object sender, EventArgs e)
    {
        ClearMPForm();
        ShowMPForm(0);
        hidMPType.Value = "125";
        hidParentMPID.Value = gvQuarter.SelectedRow.Cells[0].Text;
    }

    protected void btnNewQuarter_Click(object sender, EventArgs e)
    {
        ClearMPForm();
        ShowMPForm(0);
        hidMPType.Value = "250";
        hidParentMPID.Value = gvSemester.SelectedRow.Cells[0].Text;
    }

    protected void btnNewSemester_Click(object sender, EventArgs e)
    {
        ClearMPForm();
        ShowMPForm(0);
        hidMPType.Value = "500";
        hidParentMPID.Value = gvYear.SelectedRow.Cells[0].Text;
    }

    protected void btnNewYear_Click(object sender, EventArgs e)
    {
        ClearMPForm();
        ShowMPForm(0);
        hidMPType.Value = "1000";
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("MP");
        if(Page.IsValid)
        {
            MarkingPeriod _item = new MarkingPeriod();
            MarkingPeriodDB _db = new MarkingPeriodDB();

            _item.MPID = Convert.ToInt32(hidMPID.Value);

            _item.ParentMPID = Convert.ToInt32(hidParentMPID.Value);
            _item.SchoolID = Convert.ToInt32(Session[Global.Session_SchoolID].ToString());
            _item.Type = Convert.ToInt16(hidMPType.Value);
            _item.Title = txtTitle.Text;
            _item.ShortName = txtShortName.Text;
            _item.StartDate = Convert.ToDateTime(txtStartDate.Text);
            _item.EndDate = Convert.ToDateTime(txtEndDate.Text);
            _item.GradePostStartDate = Convert.ToDateTime(txtGPStartDate.Text);
            _item.GradePostEndDate = Convert.ToDateTime(txtGPEndDate.Text);
            _item.IsActive = true;
            _item.IsGraded = chkGraded.Checked;
            _item.IsExam = chkExam.Checked;
            _item.IsComment = chkComment.Checked;

            _item = _db.Save(_item);
            if (_item.Error.ErrorNo == 0)
            {
                hidMPID.Value = _item.MPID.ToString();
                BindMP(Convert.ToInt32(hidParentMPID.Value), Convert.ToInt16(hidMPType.Value));
            }

            lblMessage.Text = _item.Error.ErrorMessage;

        }
    }
}